﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;

namespace BDShop.Utilities.Managers
{
    public class UrlManager
    {
        public static string ProductDetailUrl(long id, string name,string []categoryDefinition)
        {
            string category = "";
            if (categoryDefinition != null)
            {
                foreach (string cat in categoryDefinition)
                {
                    category += cat.Split(':')[1] + " ";
                }
                category = SimplifyUnicodeCharacters(category.Trim());
            }
            else
            {
                category = "danh-muc";
            }

            name = SimplifyUnicodeCharacters(name);           

            var parameters = new RouteValueDictionary  
                                { 
                                    {"id", id.ToString() },
                                    {"name", name },
                                    {"category", category }
                                };
            VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "chi-tiet-san-pham", parameters);
            return vpd.VirtualPath;
        }

        public static string ResetPasswordlUrl(string un,string rt)
        {
            var parameters = new RouteValueDictionary  
                                { 
                                    {"un", un },
                                    {"rt", rt }
                                };
            VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "cap-lai-mat-khau", parameters);
            return vpd.VirtualPath;
        }

        public static string ProductOfCatUrl(int catID,string name)
        {
            name = SimplifyUnicodeCharacters(name);

            var parameters = new RouteValueDictionary  
                                { 
                                    {"catid", catID.ToString() },
                                    {"name", name }
                                };
            VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "san-pham-trong-danh-muc", parameters);
            return vpd.VirtualPath;
        }

        public static string ProductOfTagUrl(int tagID, string name)
        {
            name = SimplifyUnicodeCharacters(name);

            var parameters = new RouteValueDictionary  
                                { 
                                    {"tagid", tagID.ToString() },
                                    {"name", name }
                                };
            VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "san-pham-tu-khoa", parameters);
            return vpd.VirtualPath;
        }

        public static string ProductOfSupplierUrl(int supID, string name)
        {
            name = SimplifyUnicodeCharacters(name);

            var parameters = new RouteValueDictionary  
                                { 
                                    {"supid", supID.ToString() },
                                    {"name", name }
                                };
            VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "san-pham-nha-cung-cap", parameters);
            return vpd.VirtualPath;
        }

        public static string ProductOfBrandUrl(int brandID, string name)
        {
            name = SimplifyUnicodeCharacters(name);

            var parameters = new RouteValueDictionary  
                                { 
                                    {"brandid", brandID.ToString() },
                                    {"name", name }
                                };
            VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "san-pham-thuong-hieu", parameters);
            return vpd.VirtualPath;
        }

        /// <summary>
        /// Simplify the Unicode Characters
        /// <para></para>
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string SimplifyUnicodeCharacters(string input)
        {
            if (string.IsNullOrEmpty(input)) return String.Empty;
            input = input.Trim().ToLower();

            input = Regex.Replace(input, "[áàảãạâấầẩẫậăắằẳẵặ]", "a", RegexOptions.Singleline);
            input = Regex.Replace(input, "[đ]", "d", RegexOptions.Singleline);
            input = Regex.Replace(input, "[éèẻẽẹêếềểễệ]", "e", RegexOptions.Singleline);
            input = Regex.Replace(input, "[íìỉĩị]", "i", RegexOptions.Singleline);
            input = Regex.Replace(input, "[óòỏõọôốồổỗộơớờởỡợ]", "o", RegexOptions.Singleline);
            input = Regex.Replace(input, "[úùủũụưứừửữự]", "u", RegexOptions.Singleline);
            input = Regex.Replace(input, "[ýỳỷỹỵ]", "y", RegexOptions.Singleline);
            input = Regex.Replace(input, @"[^a-z0-9\s]+", String.Empty);
            input = input.Replace("\n", "");
            input = input.Replace("\r", "");
            input = input.Replace("\t", "");
            input = Regex.Replace(input, @" {2,}", " ", RegexOptions.IgnoreCase);
            input = input.Trim().Replace(" ", "-");

            return input;
        }
    }
}